﻿@page "/apps"

<div class="post-wrap">
    <h2 class="post-title">-&nbsp;Apps&nbsp;-</h2>
    <div class="plus-row">
        <ul>
            @if (links == null)
            {
                <Loading />
            }
            else
            {
                <li class="plus-col-xs12 plus-col-md6">
                    <NavLink target="_blank" href="https://support.qq.com/products/75616"><h3>💬~~~吐个槽_留言板~~~💬</h3></NavLink>
                </li>
                @foreach (var item in links)
                {
                    <li class="plus-col-xs12 plus-col-md6">
                        <NavLink href="@item.Url"><h3>@item.Title</h3></NavLink>
                    </li>
                }
                <li class="plus-col-xs12 plus-col-md6">
                    <NavLink @onclick="FmInit"><h3>📻~~~在线FM~~~📻</h3></NavLink>
                </li>
                <li class="plus-col-xs12 plus-col-md6">
                    <NavLink @onclick="Live2dInit"><h3>💫~~~切换 Live2d 模型~~~💫</h3></NavLink>
                </li>
            }
        </ul>
    </div>
</div>

@using System.Text.Json;

@code {
    private List<Link> links;
    protected override async Task OnInitializedAsync()
    {
        await Common.SetTitleAsync("Apps");

        links = new List<Link>
{
            new Link
            {
                Title = "✍~~~个性艺术签名设计~~~✍",
                Url = "/signature"
            },
            new Link
            {
                Title = "🎈~~~手机壁纸~~~🎈",
                Url = "/wallpaper"
            },
            new Link
            {
                Title = "🌭~~~每日热点~~~🌭",
                Url = "/hot"
            },
            new Link
            {
                Title = "🎮~~~程序员版2048~~~🎮",
                Url = "/2048"
            },
            new Link
            {
                Title = "⚫~~~五子棋~~~⚪",
                Url = "/gobang"
            },
            new Link
            {
                Title = "💣~~~扫雷~~~💣",
                Url = "/minesweeper"
            },
            new Link
            {
                Title = "🤢~~~毒鸡汤~~~🤮",
                Url = "/soul"
            },
            new Link
            {
                Title = "🎵~~~语音合成~~~🎵",
                Url = "/tts"
            },
            new Link
            {
                Title = "🔥~~~每日壁纸~~~🔥",
                Url = "/bing"
            },
            new Link
            {
                Title = "😍~~~随机妹子图~~~😍",
                Url = "/girl"
            },
            new Link
            {
                Title = "🐱~~~随机猫咪图~~~🐱",
                Url = "/cat"
            },
            new Link
            {
                Title = "⚡~~~IP地址查询~~~⚡",
                Url = "/ip2region"
            },
            new Link
            {
                Title = "👁~~~访问数据分析~~~👁",
                Url = "/analysis"
            },
            new Link
            {
                Title = "🔞~~~图集相册~~~🔞",
                Url = "/gallery"
            },
            new Link
            {
                Title = "😳~~~Emoji表情~~~😳",
                Url = "/emoji"
            },
            new Link
            {
                Title = "🔗~~~友情链接~~~🔗",
                Url = "/friendlinks"
            }
        };
    }

    private async Task Live2dInit()
    {
        await Common.InvokeAsync("window.func.live2dInit");
    }

    private async Task FmInit()
    {
        var fm_json = await Common.GetStorageAsync("fm");

        if (string.IsNullOrEmpty(fm_json))
        {
            var fm = await Http.GetFromJsonAsync<ServiceResult<IEnumerable<FMDto>>>("/fm/random");
            await Common.InvokeAsync("window.func.fmInit", fm.Result);
            return;
        }
        else
        {
            using (var json = JsonDocument.Parse(fm_json))
            {
                var expireDate = json.RootElement.GetProperty("expireDate").ToString();

                if (DateTimeOffset.UtcNow.ToUnixTimeMilliseconds() > long.Parse(expireDate))
                {
                    var fm = await Http.GetFromJsonAsync<ServiceResult<IEnumerable<FMDto>>>("/fm/random");
                    await Common.InvokeAsync("window.func.fmInit", fm.Result);
                }
                else
                {
                    await Common.InvokeAsync("window.func.fmInit", null);
                }
            }
        }
    }
}